﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Restaurant.DTO;
using Restaurant.DAO;
using System.Data;

namespace Restaurant.BUS
{
    public class BanService
    {
        private BanDB banDAO = new BanDB();

        public DataTable LoadBan()
        {
            return banDAO.LoadBan();
        }

        public DataTable LoadLoaiBan()
        {
            return banDAO.LoadLoaiBan();
        }

        public bool ThemBan(Ban b)
        {
            if (b.TenBan == "" || b.MaKhu.ToString() == "" ||b.MaKhu == -1 || b.MaLoaiBan.ToString() == ""||b.MaLoaiBan ==-1 || b.TinhTrang.ToString() == "")
            {
                return false;
            }
            if (banDAO.ThemBan(b.TenBan, b.MaKhu, b.MaLoaiBan, b.TinhTrang))
                return true;
            return false;
        }

        public bool XoaBan(int ma)
        {
            if (banDAO.XoaBan(ma))
                return true;
            return false;
        }

        public bool CapNhatBan(Ban b)
        {
            if (banDAO.CapNhapBan(b.MaBan, b.TenBan, b.MaKhu, b.MaLoaiBan, b.TinhTrang))
                return true;
            return false;
        }

        public Ban TimBanID(int ma)
        {
            Ban b = new Ban();
            DataTable dbtab = banDAO.TimBan(ma);
            DataRow dbrow = dbtab.Rows[0];
            b.MaBan = int.Parse(dbrow[0].ToString());
            b.TenBan = dbrow[1].ToString();
            b.MaKhu = int.Parse(dbrow[2].ToString());
            b.MaLoaiBan = int.Parse(dbrow[3].ToString());
            b.TinhTrang = int.Parse(dbrow[4].ToString());
            return b;
        }

        public int LayMaLoaiBan(string tenLoaiBan)
        {
            DataTable dbtab = banDAO.LayMaLoaiBan(tenLoaiBan);
            if (dbtab.Rows.Count > 0)
                return int.Parse(dbtab.Rows[0]["MaLoaiBan"].ToString());
            return -1;
        }

        public DataTable TimBanTheoNH(int manh)
        {
            DataTable dbtab = banDAO.TimBanTheoNH(manh);
            return dbtab;
        }

        public DataTable TimBanTheoKhuNH(int manh, int makhu)
        {
            DataTable dbtab = banDAO.TimBanTheoKhuNH(manh, makhu);
            return dbtab;
        }

        public Ban TimBanTheoKhuNHLoai(int manh, int makhu, int maloaiban)
        {
            Ban b = new Ban();
            DataTable dbtab = banDAO.TimBanTheoKhuNHLoai(manh, makhu, maloaiban);
            DataRow dbrow = dbtab.Rows[0];
            b.MaBan = int.Parse(dbrow[0].ToString());
            b.TenBan = dbrow[1].ToString();
            b.MaKhu = int.Parse(dbrow[2].ToString());
            b.MaLoaiBan = int.Parse(dbrow[3].ToString());
            b.TinhTrang = int.Parse(dbrow[4].ToString());
            return b;
        }

        public DataTable TimBanTheoKhuNHLoaiTinhTrang(int manh, int makhu, int maloaiban, int tinhtrang)
        {
            DataTable dbtab = banDAO.TimBanTheoKhuNHLoaiTinhTrang(manh, makhu, maloaiban, tinhtrang);
            return dbtab;
        }

        //=======
        public DataTable LoadBanDirtyreadError(int manh, int makhu, int maloaiban, int tinhtrang)
        {
            DataTable dbtab = banDAO.LoadBanDirtyreadError(manh, makhu, maloaiban, tinhtrang);
            return dbtab;
        }

        public DataTable LoadBanDirtyread(int manh, int makhu, int maloaiban, int tinhtrang)
        {
            DataTable dbtab = banDAO.LoadBanDirtyread(manh, makhu, maloaiban, tinhtrang);
            return dbtab;
        }

        public bool CapNhapBanDirtyreadError(Ban b)
        {
            if(b.MaBan.ToString()==""|| b.TenBan ==""|| b.MaKhu.ToString()==""|| b.MaLoaiBan.ToString()==""|| b.TinhTrang.ToString()=="")
                return false;
            if (banDAO.CapNhapBanDirtyreadError(b.MaBan, b.TenBan, b.MaKhu, b.MaLoaiBan, b.TinhTrang))
                return true;
            return false;
        }

        public bool CapNhapBanDirtyread(Ban b)
        {
            if (b.MaBan.ToString() == "" || b.TenBan == "" || b.MaKhu.ToString() == "" || b.MaLoaiBan.ToString() == "" || b.TinhTrang.ToString() == "")
                return false;
            if (banDAO.CapNhapBanDirtyread(b.MaBan, b.TenBan, b.MaKhu, b.MaLoaiBan, b.TinhTrang))
                return true;
            return false;
        }
    }
}
